xoopsBlockManager = Class.create();
xoopsBlockManager.prototype = {
    
  cookie: null,
  close_bid: [],
  cookie_name: 'blockCloseStatus',
  path: null,
  
  initialize: function(){
      this.cookie_name = xoops_url+this.cookie_name;
      this.applyButtons();
      this.loadBlockStatus();
      var path = xoops_url.replace(/^https?:\/\//, '');
      var slash_p = path.indexOf('/');
      if (slash_p==-1){
          this.path = '/';
      } else {
          this.path = path.substring(slash_p)+'/';
      }
  },
  
  loadBlockStatus: function(){
      var self = this;
      this.cookie = new CookieManager();
      var closeBidStr = this.cookie.getCookie(this.cookie_name);
      if (!closeBidStr){
          return;
      }
      var closeBid = closeBidStr.split(',');
      for (var i=0,j=closeBid.length; i<j; i++){
          this.closeBlock(closeBid[i]);
      }
  },
  
  closeBlock: function(bid){
      if (this.close_bid.indexOf(bid)==-1){
          this.close_bid.push(bid);
          this.cookie.setCookie(this.cookie_name, this.close_bid.toString(), this.path);
          if ($('blockTitle-'+bid)){
              $('blockTitle-'+bid).getElementsByTagName('IMG')[0].src = xoops_url+"/modules/xanhte/images/common/block_open.gif";
          }
          if ($('blockContent-'+bid)){
              Element.hide($('blockContent-'+bid));
          }
      }
  },
  
  openBlock: function(bid){
      if (this.close_bid.indexOf(bid)==-1){
          return;
      }  
      Element.show($('blockContent-'+bid));
      $('blockTitle-'+bid).getElementsByTagName('IMG')[0].src = xoops_url+"/modules/xanhte/images/common/block_close.gif";
      this.close_bid = this.close_bid.without(bid);
      this.cookie.setCookie(this.cookie_name, this.close_bid.toString(), this.path);
  },
  
  applyButtons: function(){
      var self = this;
      $A(document.getElementsByClassName('blockTitle')).each(function(title){
          title.insertBefore(self.createCloseButton(), title.lastChild);
//          title.appendChild(self.createCloseButton());
      });
  },
  
  
  createCloseButton: function(){
      var self = this;
      var img = document.createElement('IMG');
      img.src=xoops_url+"/modules/xanhte/images/common/block_close.gif";
      if (typeof(img.style.styleFloat)){
          img.style.styleFloat="right"; 
      }
      if (typeof(img.style.cssFloat)){
          img.style.cssFloat="right"; 
      }
      img.style.paddingRight="3px";
      img.style.paddingTop="3px";
      img.style.cursor="pointer";
      img['onclick'] = function(){
          var bid = img.parentNode.id.split('-', 2);
          if (bid.length==2){
              Element.hide("blockContent-"+bid[1]);
              if (self.close_bid.indexOf(bid[1])==-1){
                  self.closeBlock(bid[1]);
              } else {
                  self.openBlock(bid[1]);
              }
          }
      };
      return img;
  }
};

Event.observe(window, 'load', function(){
    xbManager = new xoopsBlockManager();
});
